package com.zhongkun.datahome.service.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongkun.datahome.core.mapper.SysRoleMapper;
import com.zhongkun.datahome.service.constant.TaskTypeEnum;
import com.zhongkun.datahome.service.domain.Task;
import com.zhongkun.datahome.service.mapper.TaskMapper;
import com.zhongkun.datahome.service.req.qry.task.TaskQry;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

/**
 * @Author: huangjie
 * @Date: 2025/4/14 17:15
 * @Desc
 */
@Repository
public class TaskRepository extends ServiceImpl<TaskMapper, Task> {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    public IPage<Task> pageTask(TaskQry qry) {
        boolean feedbackTask;
        if (Objects.nonNull(qry.getReceiveUserId())) {
            List<Long> roleUserList = sysRoleMapper.selectTaskRoleUser();
            feedbackTask = roleUserList.contains(qry.getReceiveUserId());
        } else {
            feedbackTask = false;
        }
        IPage<Task> page = new Page<>(qry.getPageNo(), qry.getPageSize());
        return this.getBaseMapper().selectPage(page, new LambdaQueryWrapper<Task>()
                .eq(Objects.nonNull(qry.getTaskId()), Task::getId, qry.getTaskId())
                .and(Objects.nonNull(qry.getReceiveUserId()), lambda -> lambda.eq(Task::getReceiveTenantUserId, qry.getReceiveUserId()).or().eq(feedbackTask, Task::getTaskType, TaskTypeEnum.FEEDBACK.getType()))
                .eq(Objects.nonNull(qry.getStartUserId()), Task::getStartTenantUserId, qry.getStartUserId())
                .eq(Objects.nonNull(qry.getTaskType()), Task::getTaskType, qry.getTaskType())
                .like(StringUtils.isNotBlank(qry.getTaskName()), Task::getTaskName, qry.getTaskName())
                .eq(Objects.nonNull(qry.getStatus()), Task::getTaskStatus, qry.getStatus()).orderByDesc(Task::getId));
    }

    public List<Task> queryReceiveTaskList(Long tenantUserId, Long familyId) {
        return this.getBaseMapper().queryReceiveTaskList(tenantUserId, familyId, isTaskRoleUser(tenantUserId));
    }

    public boolean isTaskRoleUser(Long tenantUserId) {
        List<Long> roleUserList = sysRoleMapper.selectTaskRoleUser();
        return roleUserList.contains(tenantUserId);
    }

    public List<Task> queryStartTaskList(Long tenantUserId) {
        return lambdaQuery().eq(Task::getStartTenantUserId, tenantUserId)
                .orderByDesc(Task::getId)
                .list();
    }

    public Long queryReceiveTaskCount(Long tenantUserId) {
        List<Long> roleUserList = sysRoleMapper.selectTaskRoleUser();
        return this.getBaseMapper().queryReceiveTaskCount(tenantUserId, roleUserList.contains(tenantUserId));
    }

    public List<String> queryFeedbackProcessUser() {
        return sysRoleMapper.selectTaskRoleUserName();
    }
}
